package com.mew.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

/***
 * 拉霸工具类
 * @author Administrator
 */
public class GameUtil {
    /**
     * 权重计算
     * @return
     */
    public static int weights(int[] weights){
        int[] ws = new int[weights.length];

        //总权重
        int w = 0;
        for (int i = 0; i < weights.length; i++) {
            w+=weights[i];
            ws[i] = w;
        }

        //随机一个
        int anInt = new Random().nextInt(w);
        for (int i = 0; i < ws.length; i++) {

            if (anInt<ws[i]){
                return i;
            }

        }

        //都没
        return 0;
    }

    /**
     * 权重计算
     * @return
     */
    public static int weights(long[] weights){
        long[] ws = new long[weights.length];

        //总权重
        int w = 0;
        for (int i = 0; i < weights.length; i++) {
            w += weights[i];
            ws[i] = w;
        }

        //随机一个
        int anInt = new Random().nextInt(w);
        for (int i = 0; i < ws.length; i++) {

            if (anInt<ws[i]){
                return i;
            }

        }
        //都没
        return 0;
    }

    /**
     * 打印数组
     * @param es
     */
    public static void print(int[] es){
        int row = es.length/5;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(es[i*5+j]+"   ");
            }
            System.out.println();
        }
        System.out.println();
    }
    /**
     * 打印数组 通过转入col换行
     * @param es
     */
    public static void print(int[] es,int col){
        int row = es.length/col;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(es[i*5+j]+"   ");
            }
            System.out.println();
        }
        System.out.println();
    }

    /**
     * 打印线
     * @param lines
     * @param size
     */
    public static void printLines(int[][] lines ,int size){

        for (int i = 0; i < lines.length; i++) {
            int[] line = lines[i];
            StringBuilder builder = new StringBuilder();
            int[] ints = new int[size];
            for (int f = 0; f < line.length; f++) {
                ints[line[f]] = 1;

            }
            System.out.println(i+1);
            print(ints);
            System.out.println("------------------");
        }
    }
    /**
     * -1替换成随机数
     * @param w 限制随机数大小
     */
    public static int[] getArr(int[] line,int w) {
        Random random = new Random();
        for (int i = 0; i < line.length; i++) {
            if (line[i]==-1){
                line[i] = random.nextInt(w);
            }
        }
        return line;
    }

    /**
     * 生成空奖
     */
    public static int[] getEndSlots(int w) {
        Integer[] a = {0,1,2,3,4,5,6,7};
        ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(a));
        //打乱
        Collections.shuffle(arr);

        int[] back = new int[]{
                -1,-1,-1,-1,-1,
                -1,-1,-1,-1,-1,
                -1,-1,-1,-1,-1
        };

        back[0] = arr.get(0);
        back[2] = arr.get(1);
        back[5] = arr.get(2);
        back[7] = arr.get(3);
        back[10] = arr.get(4);
        back[12] = arr.get(5);

        Random random = new Random();//生成随机数工具类
        for (int i = 0; i < back.length; i++) {
            if (-1==back[i]){
                back[i] = random.nextInt(w);
            }
        }

        return back;
    }
    /**
     * 生成空奖 4行5列
     */
    public static int[] getEndSlots45(int w) {
        Integer[] a = {0,1,2,3,4,5,6,7};
        ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(a));
        //打乱
        Collections.shuffle(arr);

        int[] back = new int[]{
                -1,-1,-1,-1,-1,
                -1,-1,-1,-1,-1,
                -1,-1,-1,-1,-1,
                -1,-1,-1,-1,-1
        };

        back[0] = arr.get(0);
        back[2] = arr.get(1);
        back[5] = arr.get(2);
        back[7] = arr.get(3);
        back[10] = arr.get(4);
        back[12] = arr.get(5);
        back[15] = arr.get(6);
        back[16] = arr.get(7);
        //生成随机数工具类
        Random random = new Random();
        for (int i = 0; i < back.length; i++) {
            if (-1==back[i]){
                back[i] = random.nextInt(w);
            }
        }

        return back;
    }
    public static  int[] iconDown(int[] iux,int[] res,int row,int col){
        if (res.length==0){
            return res;
        }
        for(int ix:iux){
            res[ix]=-1;
        }
        for (int i = 0; i < col ; i++) {
            dowm(res,row*col-i-1,row,col);
        }
        return res;
    }
    //下移
    private static void dowm(int[] res,int idx,int row,int col) {

        if (idx<0){
            return ;
        }

        if (idx<row*col-col){

            int nextIcon = res[idx+col];
            int icon = res[idx];

            if (-1==nextIcon && -1!=icon){
                res[idx+5]=icon;
                res[idx]=-1;

                if (idx+5<15){
                    dowm(res,idx+col,row,col);
                    return;
                }
            }

        }

        dowm(res,idx-col,row,col);
    }

}
